package com.kanqiubao.kq.activity.search;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.kanqiubao.kq.R;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.adapter.search.SearchTagGroupAdapter;
import com.kanqiubao.kq.fragment.SmartBaseRefreshFragment;
import com.kanqiubao.kq.fragment.main.zb.MainZbListFragment;
import com.kanqiubao.kq.fragment.recoding.RecodingListFragment;
import com.kanqiubao.kq.fragment.video.VideoListFragment;
import com.kanqiubao.kq.utils.StringUtil;
import com.kanqiubao.kq.utils.sharepreferences.SharedPreferencesUtil;
import com.kanqiubao.kq.view.tagview.TagCloudLayout;
import com.lxj.xpopup.XPopup;

import java.util.Arrays;

import androidx.fragment.app.FragmentTransaction;

public class SearchMainActivity extends BaseActivity<String> implements TextView.OnEditorActionListener {
    int mType;//0 直播,1录像,2视频
    private EditText mSearch;
    private TagCloudLayout mTagCloudLayout;
    private TextView mSstr;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mType = getIntent().getIntExtra("type", 0);
        mSearch = findView(R.id.video_search_txt_id);
        mTagCloudLayout = findView(R.id.his_tag_id);
        mSstr = findView(R.id.gy_id);
        mSearch.setOnEditorActionListener(this);
        mSearchTagGroupAdapter = new SearchTagGroupAdapter(this);
        mTagCloudLayout.setAdapter(mSearchTagGroupAdapter);
        mTagCloudLayout.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
            @Override
            public void itemClick(int position) {
                //点击时间
            }
        });
        setHisData();
    }

    @Override
    protected int getActivitylayout() {
        return R.layout.search_main_layout;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    private SearchTagGroupAdapter mSearchTagGroupAdapter;

    private void setHisData() {
        String hs = getHisData();
        if (!StringUtil.isEmptyString(hs)) {
            String[] s = hs.split(",");//分割开来
            mSearchTagGroupAdapter.addData(Arrays.asList(s));
            findViewById(R.id.his_root_id).setVisibility(View.VISIBLE);
        }
        if (mType == 0) {
            mSearch.setHint("搜索 直播|主播|房间号");
        } else if (mType == 1) {
            mSearch.setHint("搜索 录像");
        } else if (mType == 2) {
            mSearch.setHint("搜索 视频");
        }

    }

    private String getHisData() {
        String hs = "";
        if (mType == 0) {
            hs = SharedPreferencesUtil.getInstance().getSearchLive();
        } else if (mType == 1) {
            hs = SharedPreferencesUtil.getInstance().getSearchRecoding();
        } else if (mType == 2) {
            hs = SharedPreferencesUtil.getInstance().getSearchVideo();
        }
        return hs;
    }

    private void setHis(String s) {
        String hs = getHisData();
        if (StringUtil.isEmptyString(hs)) {
            hs = s;
        } else {
            hs = s + "," + hs;
        }
        if (mType == 0) {
            SharedPreferencesUtil.getInstance().setSearchLive(hs);
        } else if (mType == 1) {
            SharedPreferencesUtil.getInstance().setSearchRecoding(hs);
        } else if (mType == 2) {
            SharedPreferencesUtil.getInstance().setSearchVideo(hs);
        }
        findViewById(R.id.his_root_id).setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            String key = mSearch.getText().toString();
            if (!StringUtil.isEmptyString(key)) {
                //去搜索吧
                mSearchTagGroupAdapter.addData(key);
                setHis(key);
                search(key);
                mSstr.setVisibility(View.VISIBLE);
                mSstr.setText(String.format("关于 %s 的搜索", key));
                findViewById(R.id.search_id).setVisibility(View.VISIBLE);
            }
            return true;
        }
        return false;
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.clear_his:
                alert();
                break;
            case R.id.del:
                mSearch.setText("");
                break;
        }
        super.click(v);
    }

    private void search(String s) {
        SmartBaseRefreshFragment frag = null;
        if (mType == 0) {
            frag = new MainZbListFragment();
        } else if (mType == 1) {
            frag = new RecodingListFragment();
        } else if (mType == 2) {
            frag = new VideoListFragment();
        }
        frag.setmType(-1);
        frag.setmSearchKey(s);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.search_id, frag);
        ft.commit();
    }

    private void alert() {
        new XPopup.Builder(this).asConfirm("提示", "是否確定清除搜索记录",
                () -> {
                    //确认
                    findViewById(R.id.his_root_id).setVisibility(View.GONE);
                    setHis("");//直接設置空
                    findViewById(R.id.search_id).setVisibility(View.GONE);
                }, () -> {
                })
                .show();
    }
}
